package com.zp.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zp.mall.product.entity.BrandEntity;
import com.zp.mall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zp.mall.product.entity.CategoryBrandRelationEntity;
import com.zp.mall.product.service.CategoryBrandRelationService;
import com.zp.common.utils.PageUtils;
import com.zp.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author zhangpeng
 * @email zhangpeng@aliyun.com
 * @date 2020-10-11 17:08:57
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     *  /product/categorybrandrelation/brands/list  获取分类关联的品牌
     */
    @GetMapping("/brands/list")
    public R getRelationBrandsByCatId(@RequestParam("catId") Long catId){
        List<BrandEntity> brandEntities = categoryBrandRelationService.getRelationBrandsByCatId(catId);
        List<BrandVo> vos = brandEntities.stream ().map (brandEntity -> {
            BrandVo brandVo = new BrandVo ();
            brandVo.setBrandId (brandEntity.getBrandId ());
            brandVo.setBrandName (brandEntity.getName ());
            return brandVo;
        }).collect (Collectors.toList ());
        return R.ok ().put ("data", vos);
    }

    /**
     * 查询品牌关联的分类列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list (new QueryWrapper<CategoryBrandRelationEntity> ().eq ("brand_id", brandId));

        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveCategoryBrandRelationEntity(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
